package com.spring02.demo.testenum;

public enum ResponseStatus {

    SUCCESS(200, "成功"),
    BAD_REQUEST(400, "错误的请求"),
    UNAUTHORIZED(401, "未授权"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "未找到"),
    INTERNAL_SERVER_ERROR(500, "服务器内部错误");
    private final int code;
    private final String message;
    ResponseStatus(int code, String message) {
        this.code = code;
        this.message = message;
    }
    public int getCode() {
        return code;
    }
    public String getMessage() {
        return message;
    }
    /**
     * 根据状态码获取响应状态对象
     * @param code 状态码
     * @return 匹配的ResponseStatus枚举对象，如果没有匹配的，则返回null
     */
    public static ResponseStatus fromCode(int code) {
        for (ResponseStatus status : values()) {
            if (status.getCode() == code) {
                return status;
            }
        }
        return null;
    }
}